package cn.datawin.spider;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import cn.datawin.spider.httputil.HttpClientService;
import cn.datawin.spider.httputil.HttpRequest;
import cn.datawin.spider.httputil.HttpResponse;
import cn.datawin.spider.httputil.HttpService;
import cn.datawin.spider.httputil.HttpRequest.Method;

public class Baidu {
	public static void main(String[] args) throws Exception {
		

		HttpRequest request = new HttpRequest("http://baike.baidu.com/view/143057.htm", Method.get);
		HttpService service =  new HttpClientService();
		HttpResponse res = service.execute(request);
		FileWriter  w = new FileWriter(new File("D:/aa.htm"));
		w.append(res.getResponseString());
		System.out.println(); 
		w.close();
		
//		String url = new String("http://baike.baidu.com/view/143057.htm");
//		String fileName = new String("D:\\baike.html");
//		getContentByURL(url, fileName);
	}
	
		public static void getContentByURL(String url,String fileName) {
			InputStream is = null;
			FileOutputStream fos = null;
			try {
				URL u = new URL(url);
				is = u.openStream();
				fos = new FileOutputStream(fileName);
				byte[] b = new byte[1024];
				int len = 0;
				while ( (len = is.read(b)) != -1) {
					fos.write(b, 0, len);
				}
				System.out.println("抓取成功");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					fos.close();
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}

	}





